package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Author : wangjie
* @CreateDate : 2020/4/11 11:15
* @Version : 1.0
* @Description : 图表表现层接口实现类
*/
@Service
@Transactional
public class ReportServiceImpl implements ReportService {

    //依赖注入
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    /**
     * 获取指定月份集合中每个月的会员数据
     * @param months
     * @return
     */
    @Override
    public List<Integer> findCountByMonths(List<String> months) {
        List<Integer> memberCounts=new ArrayList<>();//创建集合存储每月会员数据
        //遍历月份集合,获取到每个月份
        for (String month : months) {
            String monthStr=month+".31";
            //调用dao 方法 获取对应的每月会员数据
            Integer memberCount= memberDao.findCountByMonthBefore(monthStr);
            memberCounts.add(memberCount);
        }
        return memberCounts;
    }

    /**
     * 获取套餐预约占比
     * @return
     */
    @Override
    public List<Map> findSetmealCount() {
        return memberDao.findSetmealCount();
    }

    /**
     * 获取运营数据
     * @return
     */
    @Override
    public Map getBusinessReportData() {
        try {
            //获得今天日期
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            //获得本周第一天日期
            String thisWeek = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获得本月第一天日期
            String thisMonth = DateUtils.parseDate2String( DateUtils.getFirstDay4ThisMonth());

            //获取与会员有关数据
            Integer todayNewMember = memberDao.findMemberCountByDate(today);
            Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeek);
            Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(thisMonth);
            Integer totalMember = memberDao.findMemberTotalCount();

            //获取与预约有关数据
            Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
            Integer thisWeekOrderNumber = orderDao.findOrderCountByAfterDate(thisWeek);
            Integer thisMonthOrderNumber = orderDao.findOrderCountByAfterDate(thisMonth);
            Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
            Integer thisWeekVisitsNumber = orderDao.findVisitsCountByAfterDate(thisWeek);
            Integer thisMonthVisitsNumber = orderDao.findVisitsCountByAfterDate(thisMonth);

            //获得套餐热门数据
            List<Map> hotSetmeal = orderDao.findHotSetmeal();

            //封装数据到map集合
            Map<String, Object> map = new HashMap<>();
            //会员有关数据
            map.put("reportDate", today);
            map.put("todayNewMember", todayNewMember);
            map.put("thisWeekNewMember", thisWeekNewMember);
            map.put("thisMonthNewMember", thisMonthNewMember);
            map.put("totalMember", totalMember);
            //预约有关数据
            map.put("todayOrderNumber", todayOrderNumber);
            map.put("thisWeekOrderNumber", thisWeekOrderNumber);
            map.put("thisMonthOrderNumber", thisMonthOrderNumber);
            map.put("todayVisitsNumber", todayVisitsNumber);
            map.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
            map.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
            //套餐热门数据
            map.put("hotSetmeal", hotSetmeal);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
